import {Component,ChangeDetectorRef} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import * as moment from 'moment';

import {ScheduleAddProvider} from '../../../providers/schedule/schedule-add/schedule-add.provider';

@IonicPage()
@Component({
    selector: 'page-schedule-end-repeat',
    templateUrl: 'schedule-end-repeat.html',
    providers: [
        ScheduleAddProvider
    ]
})
export class ScheduleEndRepeatPage {

    minTime: string;

    endRepeatTime: string;
    // 年度列表
    private yearList:any[]=[];

    // 结束类型
    private endType:number;

    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                private scheduleAddProvider:ScheduleAddProvider,
                private changeDetectorRef:ChangeDetectorRef
                ) {

    }

    ionViewDidEnter() {
        this.yearList=this.scheduleAddProvider.getYearList(moment(new Date(this.navParams.get('endTime'))).year());
        this.minTime = this.navParams.get('endTime');
        console.log(this.navParams.get('endRepeat'),this.minTime);
        if(this.navParams.get('endRepeat')!=='永不'){
            this.endRepeatTime = moment(this.navParams.get('endRepeat')*1000).format('YYYY-MM-DD');
            this.endType=2;
        }else{
            this.endType=1;
        }
    }

    clearEndRepeat() {
        setTimeout(()=>{
            this.endType=1;
            this.changeDetectorRef.detectChanges();
        },100)
    }

    selEnd(){
        setTimeout(()=>{
            this.endType=2;
            this.changeDetectorRef.detectChanges();
        },100)
        // this.navCtrl.pop().then(()=>{
        //     this.navCtrl.setPages([{page: 'ScheduleAddPage', params: {'endRepeat': '123'}}])
        // })
    }

    saveHandler(){
        if(this.endType==1){
            this.navCtrl.getPrevious().instance.setEndRepeat('永不');
            this.navCtrl.pop();
        }
        if(this.endType==2&&typeof moment(this.endRepeatTime).unix()=='number'){
            this.navCtrl.getPrevious().instance.setEndRepeat(moment(this.endRepeatTime).unix());
            this.navCtrl.pop();
        }
    }

    backButtonClick(ev) {
        ev.preventDefault();
        ev.stopPropagation();
        this.navCtrl && this.navCtrl.pop(null, null);
    }

}
